/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-04-27
 * Time: 23:02
 */
public class MySingleLinkedList {

    static class ListNode{
        private int value;//结点的值
        private ListNode next;//存放下一个结点的地址
        //初始化
        public ListNode(int data) {
            this.value = data;
        }
    }
    public ListNode head;

    public void createNode() {
        ListNode node1 = new ListNode(5);
        ListNode node2 = new ListNode(6);
        ListNode node3 = new ListNode(8);
        ListNode node4 = new ListNode(9);
        ListNode node5 = new ListNode(85);
        ListNode node6 = new ListNode(96);
        //将链表连起来
        head = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;
        node6.next = null;
    }

    //打印单链表
    public void display() {
        ListNode cur = head;
        while(cur!=null) {
            System.out.print(cur.value+"->");
            cur =cur.next;
        }
        System.out.println("null");
    }

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);//创建一个新的节点将它插入到尾部
        node.next = head;
        head = node;
    }
    //尾插法
    public void addLast(int data){
       ListNode node = new ListNode(data);//创建一个新的节点将它插入到尾部
       ListNode cur = this.head;
       if(this.head==null){
           this.head = node;
       }else {
           //先找到尾
           while(cur.next!=null) {
               cur =cur.next;
           }
           cur.next = node;
       }
    }
    //检查下标是否合法
    private void checkIndexAdd(int index) {
        if(index<0||index>size()) {
            throw new IllegalArgumentException("index下标不合法！！！");
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        checkIndexAdd(index);
        if(index==0) {
            addFirst(data);
        } else if(index == size()-1) {
            addLast(data);
        } else {
            //中间插入
            ListNode node = new ListNode(data);
            ListNode cur = head;
            //先走index-1步早到要插入的位置的前一个位置
            while (index - 1 != 0) {
                cur = cur.next;
                index--;
            }
            //此时已经找到前一个位置
            node.next = cur.next;
            cur.next = node;
        }
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.value == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
         ListNode cur = head;

    }
    //删除所有值为key的节点
    public void removeAllKey(int key){

    }
    //得到单链表的长度
    public int size(){
        int count =0;
        ListNode cur =head;
        while(cur!=null) {
            count++;
            cur =cur.next;
        }
        return count;
    }
    public void clear(){

    }


}
